package com.ruoyi.web.controller.workflow;

import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workflow.domain.ServiceItem;
import com.ruoyi.workflow.service.IServiceItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.workflow.constant.AuditConstant.SI_SALE_STATUS_DOWN;
import static com.ruoyi.workflow.constant.AuditConstant.SI_SALE_STATUS_UP;

/**
 * 服务项Controller
 * 
 * @author xiaodu
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/workflow/serviceItem")
public class ServiceItemController extends BaseController
{
    @Autowired
    private IServiceItemService serviceItemService;

    /**
     * 发起审核
     */
    @PreAuthorize("@ss.hasPermi('workflow:serviceItem:startAudit')")
    @PostMapping("startAudit")
    public AjaxResult startAudit(Long id,String packageAuditInfo)  {
        serviceItemService.startAudit(id,packageAuditInfo);
        return AjaxResult.success("已发起套餐审核");
    }

    /**
     * 服务项上架
     */
    @PreAuthorize("@ss.hasPermi('workflow:serviceItem:saleOn')")
    @PutMapping("saleOn/{id}")
    public AjaxResult saleOn(@PathVariable Long id){
        serviceItemService.changeSaleStatus(id,SI_SALE_STATUS_UP);
        return AjaxResult.success("已上架");
    }

    /**
     * 服务项下架
     */
    @PreAuthorize("@ss.hasPermi('workflow:serviceItem:saleOff')")
    @PutMapping("saleOff/{id}")
    public AjaxResult saleOff(@PathVariable Long id){
        serviceItemService.changeSaleStatus(id,SI_SALE_STATUS_DOWN);
        return AjaxResult.success("已下架");
    }

    /**
     * 查询服务项列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:serviceItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServiceItem serviceItem)
    {
        startPage();
        List<ServiceItem> list = serviceItemService.selectServiceItemList(serviceItem);
        return getDataTable(list);
    }


    /**
     * 获取服务项详细信息
     */
    @PreAuthorize("@ss.hasPermi('workflow:serviceItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(serviceItemService.selectServiceItemById(id));
    }

    /**
     * 新增服务项
     */
    @PreAuthorize("@ss.hasPermi('workflow:serviceItem:add')")
    @Log(title = "服务项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceItem serviceItem)
    {
        return toAjax(serviceItemService.insertServiceItem(serviceItem));
    }

    /**
     * 修改服务项
     */
    @PreAuthorize("@ss.hasPermi('workflow:serviceItem:edit')")
    @Log(title = "服务项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceItem serviceItem)
    {
        return toAjax(serviceItemService.updateServiceItem(serviceItem));
    }
}
